/*
 * Copyright (C) 2012 by Zhang Li <RichSelian at gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the project nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#include "nw-client.h"
#include "nw-request.h"

int nw_client_on_recv(nw_client_t* client) {
    size_t recvlen;

    client->m_active_time = time(NULL);
    if(client->m_ioffset + 1 < sizeof(client->m_ibuf)) {
        if((recvlen = recv(client->m_connsock, client->m_ibuf + client->m_ioffset, client->m_ilen - client->m_ioffset, 0)) == -1) {
            if(errno == EAGAIN) {
                client->m_ilen = client->m_ioffset;
                nw_do_request(client);
                return 0;
            } else if(errno == EWOULDBLOCK) {
                return 0;
            } else {
                return -1;
            }
        }
        client->m_ioffset += recvlen;
        return 0;
    }
    client->m_ilen = 0; /* recvdata too long, ignore some data and that will cause a '400 Bad Request'. */
    return 0;
}

int nw_client_on_send(nw_client_t* client) {
    size_t sendlen;

    client->m_active_time = time(NULL);
    if(client->m_ooffset < client->m_olen) { /* send response head */
        if((sendlen = send(client->m_connsock, client->m_obuf + client->m_ooffset, client->m_olen - client->m_ooffset, 0)) == -1) {
            if(errno == EWOULDBLOCK) {
                return 0;
            } else {
                return -1;
            }
        }
        client->m_ooffset += sendlen;
        return 0;
    }
    if(client->m_fd != -1) { /* sending file */
        if(client->m_fd_offset == client->m_fd_len) {
            if(client->m_close_after_sent) {
                return -1;
            }
            return 0;
        }
        if((sendlen = sendfile(client->m_connsock, client->m_fd, &client->m_fd_offset, client->m_fd_len - client->m_fd_offset)) == -1) {
            if(errno == EWOULDBLOCK) {
                return 0;
            } else {
                return -1;
            }
        }

    }
    return 0;
}
